package com.example.cloudtrade;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.cloudtrade.fragment.AddFragment;
import com.example.cloudtrade.fragment.HomeFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class AddActivity2 extends AppCompatActivity {

    private Button androidButton;
    private Button iosButton;
    private Button qqAccountButton;
    private Button wechatAccountButton;
    private Button qqPriorityAccountButton;
    private Button wechatTestAccountButton;

    // 用于标记操作系统分组当前选中的按钮
    private Button selectedOsButton;
    // 用于标记商品类别分组当前选中的按钮
    private Button selectedCategoryButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_add2);

        // 获取各个按钮实例
        androidButton = findViewById(R.id.android_button);
        iosButton = findViewById(R.id.ios_button);
        qqAccountButton = findViewById(R.id.qq_account_button);
        wechatAccountButton = findViewById(R.id.微信账号_button);
        qqPriorityAccountButton = findViewById(R.id.qq_priority_account_button);
        wechatTestAccountButton = findViewById(R.id.微信体验服账号_button);

        setButtonClickListeners();

        // 点击跳转事件
        Button button = findViewById(R.id.next_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 检查两个分组是否都有选中项
                if (selectedOsButton != null && selectedCategoryButton != null) {
                    Intent intent = new Intent(AddActivity2.this, AddActivity3.class);
                    startActivity(intent);
                } else {
                    // 提示用户
                    Toast.makeText(AddActivity2.this, "请选择操作系统和商品类别", Toast.LENGTH_SHORT).show();
                }
            }
        });

        ImageView imageView = findViewById(R.id.back_arrow);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(AddActivity2.this, AddActivity.class);
//                startActivity(intent);
                finish();
            }
        });
    }

    // 选中变色
    private void setButtonClickListeners() {
        Button[] buttons = {androidButton, iosButton, qqAccountButton, wechatAccountButton, qqPriorityAccountButton, wechatTestAccountButton};

        for (Button button : buttons) {
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String buttonTag = button.getTag()!= null? button.getTag().toString() : "";
                    // 根据按钮的tag来判断所属分组，进行不同分组内的单选逻辑处理
                    if ("os".equals(buttonTag)) {
                        // 操作系统分组
                        if (selectedOsButton!= null && selectedOsButton!= button) {
                            selectedOsButton.setBackgroundTintList(getResources().getColorStateList(android.R.color.white));
                        }
                        selectedOsButton = button;
                    } else if ("type".equals(buttonTag)) {
                        // 商品类别分组
                        if (selectedCategoryButton!= null && selectedCategoryButton!= button) {
                            selectedCategoryButton.setBackgroundTintList(getResources().getColorStateList(android.R.color.white));
                        }
                        selectedCategoryButton = button;
                    }
                    button.setBackgroundTintList(getResources().getColorStateList(R.color.selected_color));
                }
            });
        }
    }
}